<?php


class User_Model_Solicituds extends Zend_Db_Table_Abstract
{
    protected $_name = 'solicituds';
    protected $_primary = 'id_solicitud';
 
    public function obte_peticions_diferides($mail){
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('mail_user = "'.$mail.'" and dia IS NOT NULL');
        $result = $select->query()->fetchAll();
        
        if(sizeof($result) == 0)    return -1;      // No hi ha peticions diferides
        
        $ret = array();
        for($i = 0; $i < sizeof($result); $i++){
            if($result[$i]['mail_taxi'] == '0'){        // Solicitud no acceptada
                $ret[] = array($result[$i]['id_solicitud'], 0);
            } else {                                    // Solicitud acceptada
                $select = $this->_db->select()
                    ->from('taxistes')
                    ->where('mail = "'.$result[$i]['mail_taxi'].'"'); 
                $result2 = $select->query()->fetchAll();
                
                $ret[] = array($result[$i]['id_solicitud'], $result2[0]['nom']);
                
            }

        }
        
        return $ret;
        
    }
    
    public function obte_mail_taxi($id_sol){
        $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('id_solicitud = "'.$id_sol.'"');
        $result = $select->query()->fetchAll();
        if(sizeof($result) == 0)    return -1;
        return $result[0]['mail_taxi'];
    }
    
    public function fer_solicitud($mail, $posX, $posY, $dia = NULL, $hora = NULL){
        $data = array(
            'mail_user' =>  $mail,
            'posx'      =>  $posX,
            'posy'      =>  $posY,
            'dia'       =>  $dia,
            'hora'      =>  $hora,
            'mail_taxi' =>  '0'
        );
        return $this->insert($data);
    }
   
    public function cancelar_solicitud($id){

        return $this->delete("id_solicitud = ".$id);
    }

}
?>
